package com.gitee.wsl.base


interface ItemFlagAction<T,FLAG>{
    fun isItemFlagSet(id: T):Boolean

    fun getItemFlag(id: T): FLAG

    fun getItemFlagList(): List<FLAG>

    fun count():Int

    fun onDataItemRemoved(id: T)

    fun reset()

    fun ableFlag(id:T,vararg flagValue:Int)

    fun disableFlag(id:T,vararg flagValue:Int)

    fun hasFlag(id:T,flagValue:Int):Boolean

    fun hasNotSet(id:T):Boolean

    fun ableAll(id:T)

    fun disableAll(id:T)
}

open class ItemFlagManger<T>:ItemFlagAction<T,ItemFlag<T>>{
    private val itemFlagList = mutableListOf<ItemFlag<T>>()
    override fun isItemFlagSet(id: T): Boolean {
        return itemFlagList.contains(ItemFlag(id))
    }

    override fun getItemFlag(id: T): ItemFlag<T> {
        val index=itemFlagList.indexOf(ItemFlag(id))
        if(index==-1){
            val flag=ItemFlag(id)
            itemFlagList.add(flag)
            return flag
        }
        return itemFlagList[index]
    }

    override fun getItemFlagList(): List<ItemFlag<T>> {
        return itemFlagList
    }

    override fun count(): Int {
        return itemFlagList.size
    }

    override fun onDataItemRemoved(id: T) {
        itemFlagList.remove(ItemFlag(id))
    }

    override fun reset() {
        itemFlagList.clear()
    }

    override fun ableFlag(id: T, vararg flagValue: Int) {
        getItemFlag(id).ableFlagArray(flagValue)
    }

    override fun disableFlag(id: T, vararg flagValue: Int) {
        getItemFlag(id).disableFlagArray(flagValue)
    }

    override fun hasFlag(id: T, flagValue: Int): Boolean {
        return if(isItemFlagSet(id)) getItemFlag(id).hasFlag(flagValue) else false
    }

    override fun hasNotSet(id: T): Boolean {
        return if(isItemFlagSet(id)) getItemFlag(id).hasNotSet() else true
    }

    override fun ableAll(id: T) {
        getItemFlag(id).ableAll()
    }

    override fun disableAll(id: T) {
        getItemFlag(id).disableAll()
    }
}

/*
class PositionFlagManger:ItemFlagAction<Int,Flag>{

    private val selectedItems = SparseArray<Flag>()

    override fun isItemFlagSet(id: Int): Boolean {
       return selectedItems.indexOfKey(id)>0
    }

    override fun getItemFlag(id: Int): Flag {
        if(isItemFlagSet(id))
            return selectedItems.get(id)
        val newFlag=Flag()
        selectedItems.put(id, newFlag)
        return newFlag
    }

    override fun getItemFlagList(): List<Flag> {
        val itemSize = selectedItems.size()
        val items: MutableList<Flag> = ArrayList(itemSize)
        for (i in 0 until itemSize) {
            val position=selectedItems.keyAt(i)
            val itemFlag=selectedItems.get(position)
            items.add(itemFlag)
        }
        return items
    }

    fun getItemFlagMap(): Map<Int,Flag> {
        val itemSize = selectedItems.size()
        val items= mutableMapOf<Int,Flag>()
        for (i in 0 until itemSize) {
            val position=selectedItems.keyAt(i)
            val itemFlag=selectedItems.get(position)
            items[position] = itemFlag
        }
        return items
    }

    override fun count(): Int {
        return selectedItems.size()
    }

    override fun onDataItemRemoved(id: Int) {
        selectedItems.remove(id)
    }

    override fun reset() {
        selectedItems.clear()
    }

    override fun ableFlag(id: Int, vararg flagValue: Int) {
        flagValue.forEach {
            getItemFlag(id).ableFlag(it)
        }

    }

    override fun disableFlag(id: Int, vararg flagValue: Int) {
        flagValue.forEach {
            getItemFlag(id).disableFlag(it)
        }

    }

    override fun hasFlag(id: Int, flagValue: Int): Boolean {
        return getItemFlag(id).hasFlag(flagValue)
    }

    override fun hasNotSet(id: Int): Boolean {
         if(isItemFlagSet(id))
             return getItemFlag(id).hasNotSet()
        return false
    }

    override fun ableAll(id: Int) {
        getItemFlag(id).ableAll()
    }

    override fun disableAll(id: Int) {
        getItemFlag(id).disableAll()
    }

}*/
